package cn.signalpha.modules.social.controller.api;

import cn.signalpha.common.annotation.FormRepeatCommitAnnotation;
import cn.signalpha.common.constant.AppConstant;
import cn.signalpha.common.constant.MsgConstant;
import cn.signalpha.common.utils.Assert;
import cn.signalpha.core.boot.ctrl.BaseController;
import cn.signalpha.core.mp.support.Condition;
import cn.signalpha.core.mp.support.Query;
import cn.signalpha.core.tool.api.R;
import cn.signalpha.core.tool.utils.ObjectUtil;
import cn.signalpha.modules.com.entity.User;
import cn.signalpha.modules.com.service.UserService;
import cn.signalpha.modules.social.dto.BlacklistDTO;
import cn.signalpha.modules.social.entity.Blacklist;
import cn.signalpha.modules.social.service.BlacklistService;
import cn.signalpha.modules.social.vo.BlacklistVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.Map;

/**
 * 控制器
 *
 * @author signalpha
 */
@RestController
@AllArgsConstructor
@RequestMapping(AppConstant.APPLICATION_NAME_PREFIX + "biz/wap/blacklist")
@Api(value = "黑名单", tags = "黑名单")
public class ApiBlacklistController extends BaseController {

	private final UserService userService;
	private final BlacklistService blacklistService;

    @GetMapping("/page")
    @ApiOperation(value = "黑名单列表", notes = "")
    public R<IPage<BlacklistVO>> page(@ApiIgnore @RequestParam Map<String, Object> params, Query query) {
		params.put("userId", getUser().getUserId().toString());
        return R.data(blacklistService.selectBlacklistPage(Condition.getPage(query), params));
    }

	@PostMapping("/save")
	@FormRepeatCommitAnnotation
	@ApiOperationSupport(order = 10)
	@ApiOperation(value = "拉黑", notes = "")
	public R save(@Valid @RequestBody BlacklistDTO fansDTO) {
		Long userId = getUser().getUserId();
		User user = userService.getById(fansDTO.getUserId());
		Assert.isNull(user, MsgConstant.ACCOUNT_NULL);
		Assert.isFalse(fansDTO.getUserId().equals(userId), "不可自己拉黑自己");

		Blacklist blacklist =  blacklistService.getByUserIdAndTargetId(userId, fansDTO.getUserId());
		if(ObjectUtil.isEmpty(blacklist)){
			blacklist = Blacklist.builder().userId(userId).targetId(fansDTO.getUserId()).build();
			blacklistService.save(blacklist);
		}
		return R.status(true);
	}

	@PostMapping("/remove")
	@FormRepeatCommitAnnotation
	@ApiOperationSupport(order = 11)
	@ApiOperation(value = "取消拉黑", notes = "")
	public R remove(@Valid @RequestBody BlacklistDTO fansDTO) {
		return R.status(blacklistService.remove(Wrappers.<Blacklist>lambdaQuery().eq(Blacklist::getUserId, getUser().getUserId()).eq(Blacklist::getTargetId, fansDTO.getUserId())));
	}

}
